package com.ca.ui.core.view.card;

import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.ca.ui.core.R;
import com.ca.ui.core.adapter.PaperHistoryAdapter;
import com.ca.ui.core.bean.PaperInfo;

import java.util.ArrayList;
import java.util.List;

public class PaperHistoryCard extends ConstraintLayout {

    public TextView labelView;
    public ImageView rightView;
    public TextView emptyView;
    public RecyclerView rv;
    private final PaperHistoryAdapter adapter = new PaperHistoryAdapter(null);

    private final List<PaperInfo> list = new ArrayList<>();

    public PaperHistoryCard(@NonNull Context context) {
        super(context);
        initView(context);
    }

    public PaperHistoryCard(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public PaperHistoryCard(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    private void initView(Context context) {

        View view = LayoutInflater.from(context).inflate(R.layout.paper_history_card_layout, this);

        labelView = view.findViewById(R.id.tv_label);
        rightView = view.findViewById(R.id.iv_right);
        emptyView = view.findViewById(R.id.tv_empty);
        rv = view.findViewById(R.id.rv);

        rv.setLayoutManager(new LinearLayoutManager(context) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        });
        rv.setAdapter(adapter);
    }

    public List<PaperInfo> getList() {
        return list;
    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    public void setList(List<PaperInfo> list) {

        if (list == null) {
            list = new ArrayList<>();
        }

        //  按照时间排序
        list.sort((o1, o2) -> o2.getTime().compareTo(o1.getTime()));
        //  提取前7个
        this.list.clear();
        for (int i = 0; i < list.size(); i++) {
            if (i == 7) {
                break;
            }
            this.list.add(list.get(i));
        }
        adapter.setList(this.list);
    }
}
